package com.pxxy.mall.product.app;

import java.util.Arrays;
import java.util.List;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.pxxy.mall.product.entity.CategoryEntity;
import com.pxxy.mall.product.service.CategoryService;
import com.pxxy.common.utils.R;



/**
 * 商品三级分类
 *
 * @author Zhangjj
 * @email zhangjianjian@gmail.com
 * @date 2023-11-29 17:05:08
 */
@Slf4j
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查出所有的分类以及子分类，以树形的结构组装起来
     */
    @GetMapping("/list/tree")
    public R list(){
        List<CategoryEntity> entities = categoryService.listWithTree();
        return R.ok().put("data", entities);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 批量保存
     */
    @PostMapping("/update/sort")
    public R updateSort(@RequestBody CategoryEntity[] category ){
        this.categoryService.updateBatchById(Arrays.asList(category));
        return R.ok();
    }


    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody CategoryEntity category){
        System.out.println(category);
		categoryService.updateDetail(category);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] catIds){
		categoryService.removeByIds(Arrays.asList(catIds));
        return R.ok();
    }




}
